package com.banksoft.admin.controller;


import com.banksoft.admin.bean.Status;
import com.banksoft.admin.entity.Admin;
import com.banksoft.admin.service.AdminService;
import com.banksoft.admin.service.BaseService;
import com.banksoft.admin.service.RoleService;
import com.banksoft.common.BaseController;
import com.banksoft.common.PageInfo;
import com.banksoft.util.MD5Util;
import com.banksoft.util.StringUtil;
import com.banksoft.util.UUIDFactory;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2016/4/6.
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Autowired
    AdminService adminService;
    @Autowired
    RoleService roleService;
    @Autowired
    BaseService baseService;

    @RequestMapping("/list")
    public String list(){
        return "admin/admin_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(String loginName,String status,PageInfo page){
       PageInfo pageInfo = this.adminService.listAdmin(loginName,status,page);

        return this.getPageResult(pageInfo);
    }

    @RequestMapping("/beforeUpdatePwd")
    public String beforeUpdatePwd(){

        return "admin/update_pwd";
    }

    @RequestMapping("/updatePwd")
    @ResponseBody
    public JSONObject updatePwd(String old_pwd,String new_pwd,HttpServletRequest request,Model model){
        Admin  admin = (Admin) this.getCurrentUser(request);
        if(admin.getPassword().equals(MD5Util.getMD5ofStr(old_pwd))){
            admin.setPassword(MD5Util.getMD5ofStr(new_pwd));
            this.adminService.updateAdmin(admin);
            return this.FmtResult(true,"更新成功",null);
        }else{
            return this.FmtResult(false,"更新失败",null);
        }
    }

    @RequestMapping("/beforeEdit")
    public String beforeEdit(String id,Model model){
        if(StringUtil.isNotNull(id)){
            Admin admin = this.adminService.get(id);
            model.addAttribute("admin",admin);
        }
        List list = this.roleService.listRole();
        model.addAttribute("list",list);

        return "admin/edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(Admin admin,HttpServletRequest request){
        try {
            List<Admin> adminlist = this.adminService.listAdminbyName(admin.getLoginName());
            if (StringUtil.isNotEmpty(adminlist)) {
                Admin dbadmin = adminlist.get(0);
                if (StringUtil.isNotNull(admin.getId())) {
                    if(!admin.getId().equals(dbadmin.getId())){
                        return this.FmtResult(false, "已存在相同名称的管理员", null);
                    }
                }else{
                    return this.FmtResult(false, "已存在相同名称的管理员", null);
                }
            }
            if (StringUtil.isNotNull(admin.getId())) {
                this.saveLog("更新管理员"+admin.getLoginName()+"("+admin.getId()+")信息",request);
                adminService.updateAdmin(admin);
            } else {
                    admin.setId(UUIDFactory.random());
                    admin.setPassword(MD5Util.getMD5ofStr(admin.getPassword()));
                    admin.setLoginCnt(0);
                    admin.setStatus(Status.NORMAL.name());
                    this.adminService.saveAdmin(admin);

                    this.saveLog("保存管理员 "+admin.getLoginName(),request);

            }

            return this.FmtResult(true, "保存成功", null);
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,"保存失败",null);
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public JSONObject updateStatus(String id,String status,HttpServletRequest request){
        try {
            Admin admin = this.adminService.get(id);
            if(admin!=null) {
                admin.setStatus(status);
                String content = "冻结 "+admin.getLoginName();
                if(status.equals("NORMAL")){
                    content = "解冻 "+admin.getLoginName();
                }
                this.adminService.updateAdmin(admin);
                this.saveLog(content,request);
                return this.FmtResult(true,"操作成功",null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return this.FmtResult(false,"操作失败",null);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONObject delete(String id){
        try{
            this.adminService.delete(id);
            return this.FmtResult(true,"删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return this.FmtResult(false,"删除失败"+e.getMessage(),null);
        }
    }

}
